package com.bawei.mall.product.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.bawei.common.core.domain.PageResult;
import com.bawei.common.core.domain.R;
import com.bawei.common.core.domain.Result;
import com.bawei.mall.product.domain.MallProductAddrInfo;
import com.bawei.mall.product.domain.MallProductInfoNew;
import com.bawei.mall.product.domain.MallProductUserInfo;
import com.bawei.mall.product.domain.reponse.ProductDetailsNewResponse;
import com.bawei.mall.product.domain.reponse.ProductDetailsResponse;
import com.bawei.mall.product.domain.reponse.ProductInfoResponse;
import com.bawei.mall.product.domain.request.ProductInfoRequest;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.bawei.common.log.annotation.Log;
import com.bawei.common.log.enums.BusinessType;
import com.bawei.common.security.annotation.RequiresPermissions;
import com.bawei.mall.product.domain.MallProductInfo;
import com.bawei.mall.product.service.IMallProductInfoService;
import com.bawei.common.core.web.controller.BaseController;
import com.bawei.common.core.web.domain.AjaxResult;
import com.bawei.common.core.utils.poi.ExcelUtil;
import com.bawei.common.core.web.page.TableDataInfo;

/**
 * 商品信息Controller
 *
 * @author DongZeLiang
 * @date 2022-09-19
 */
@RestController
@RequestMapping("/info")
@Api(tags = "商品维护 - API")
public class MallProductInfoController extends BaseController {
    @Autowired
    private IMallProductInfoService mallProductInfoService;

    /**
     * 查询商品信息列表
     */
    @RequiresPermissions("product:info:list")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    public TableDataInfo list(MallProductInfo mallProductInfo) {
        startPage();
        List<MallProductInfo> list = mallProductInfoService.selectMallProductInfoList(mallProductInfo);
        return getDataTable(list);
    }

    @PostMapping("/syncList")
    public TableDataInfo syncList(@RequestBody MallProductInfo mallProductInfo) {
        startPage();
        List<MallProductInfo> list = mallProductInfoService.selectMallProductInfoList(mallProductInfo);
        return getDataTable(list);
    }

    /**
     * 查询商品信息总条数
     */
    @RequiresPermissions("product:info:list")
    @PostMapping("/count")
    public R<Long> count(@RequestBody MallProductInfo mallProductInfo) {
        return R.ok(mallProductInfoService.selectMallProductInfoCount(mallProductInfo));
    }


    /**
     * 导出商品信息列表
     */
    @RequiresPermissions("product:info:export")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallProductInfo mallProductInfo) {
        List<MallProductInfo> list = mallProductInfoService.selectMallProductInfoList(mallProductInfo);
        ExcelUtil<MallProductInfo> util = new ExcelUtil<MallProductInfo>(MallProductInfo.class);
        util.exportExcel(response, list, "商品信息数据");
    }

    /**
     * 获取商品信息详细信息
     */
    @RequiresPermissions("product:info:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(mallProductInfoService.selectMallProductInfoById(id));
    }

    /**
     * 上架商品
     */
    @GetMapping(value = "/upload/{id}/{isUpload}")
    @ApiOperation("上架商品")
    public R uploadInfo(@PathVariable("id") Long id, @PathVariable("isUpload") boolean isUpload) {
        mallProductInfoService.uploadInfo(id,isUpload);
        return R.ok();
    }

    @GetMapping(value = "/result/{id}")
    public R<ProductInfoResponse> getResultInfo(@PathVariable("id") Long id) {
        return R.ok(mallProductInfoService.selectMallProductInfoById(id));
    }

    /**
     * 获取商品详情
     *
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    public R<ProductDetailsResponse> getProductResponse(@PathVariable("id") Long id) {
        return R.ok(mallProductInfoService.selectProductDetailsById(id));
    }

    /**
     * 新增商品信息
     */
    @RequiresPermissions("product:info:add")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("商品添加")
    public AjaxResult add(@RequestBody @ApiParam("商品请求实体类") ProductInfoRequest productInfoRequest) {
        return toAjax(mallProductInfoService.insertMallProductInfo(productInfoRequest));
    }

    /**
     * 修改商品信息
     */
    @RequiresPermissions("product:info:edit")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductInfoRequest productInfoRequest) {
        return toAjax(mallProductInfoService.updateMallProductInfo(productInfoRequest));
    }

    /**
     * 删除商品信息
     */
    @RequiresPermissions("product:info:remove")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mallProductInfoService.deleteMallProductInfoByIds(ids));
    }


    /**
     * 修改商品状态
     * @param mallProductInfo
     * @return
     */
    @PostMapping("/updStatus")
    public R updStatus(@RequestBody MallProductInfo mallProductInfo) {
        mallProductInfoService.updStatus(mallProductInfo);
        return R.ok();
    }


    /**
     * 猜你喜欢列表
     */
    @GetMapping("/guessYouLike")
    public R<PageInfo<MallProductInfoNew>> guessYouLike(@RequestParam(value = "pageNum") Integer pageNum,
                                                            @RequestParam(value = "pageSize") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MallProductInfoNew> productInfoList = mallProductInfoService.guessYouLike(pageNum,pageSize);
        PageInfo<MallProductInfoNew> mallProductInfoPageInfo = new PageInfo<>(productInfoList);
        return R.ok(mallProductInfoPageInfo);
    }


    /**
     * 根据品牌ID查询商品
     */
    @GetMapping("/productInfoByBrandId")
    public Result<PageResult<MallProductInfoNew>> productInfoByBrandId(@RequestParam("brandId") Integer brandId,
                                                                    @RequestParam("pageNum") Integer pageNum,
                                                                    @RequestParam("pageSize") Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<MallProductInfoNew> mallProductInfoList = mallProductInfoService.productInfoByBrandId(brandId);
        PageInfo<MallProductInfoNew> mallProductInfoPageInfo = new PageInfo<>(mallProductInfoList);
        Result<PageResult<MallProductInfoNew>> result = PageResult.toResult(mallProductInfoPageInfo.getTotal(), mallProductInfoList);
        return result;
    }


    /**
     * 商品详情
     */
    @GetMapping("/detail/{id}")
    public R<ProductDetailsNewResponse> detail(@PathVariable("id") Long id) {
        ProductDetailsNewResponse productDetailsNewResponse = mallProductInfoService.detail(id);
        return R.ok(productDetailsNewResponse);
    }
//    public R<HashMap<String, Object>> detail(@PathVariable("id") Long id) {
//        HashMap<String, Object> map = mallProductInfoService.detail(id);
//        return R.ok(map);
//    }
////    public R detail(@PathVariable("id") Long id) {
////        HashMap<String, Object> map = mallProductInfoService.detail(id);
////        return R.ok(map);
////    }


    /**
     * 查询用户有没有关注这个品牌
     * @param brandId
     * @return
     */
    @GetMapping("/selectUser")
    public R<MallProductUserInfo> detail(@RequestParam("brandId") Integer brandId) {
        MallProductUserInfo mallProductUserInfo = mallProductInfoService.selectMallProductUserInfo(brandId);
        return R.ok(mallProductUserInfo);
    }

    /**
     * 添加关注
     * @param mallProductUserInfo
     * @return
     */
    @PostMapping("/addCollection")
    public R addCollection(@RequestBody MallProductUserInfo mallProductUserInfo) {
        mallProductInfoService.addCollection(mallProductUserInfo);
        return R.ok();
    }

    /**
     * 取消关注
     * @param brandId
     * @return
     */
    @PostMapping("/deleteCollection")
    public R deleteCollection(@RequestParam("brandId") Integer brandId) {
        mallProductInfoService.deleteCollection(brandId);
        return R.ok();
    }

    /**
     * 查询关注列表
     */
    @GetMapping("/fetchCollectionList")
    public R<PageInfo<MallProductUserInfo>> fetchCollectionList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                 @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MallProductUserInfo> mallProductUserInfoList = mallProductInfoService.selectMallProductUserInfoList(pageNum,pageSize);
        PageInfo<MallProductUserInfo> mallProductUserInfoPageInfo = new PageInfo<>(mallProductUserInfoList);
        return R.ok(mallProductUserInfoPageInfo);
    }

    /**
     * 清空关注列表
     */
    @PostMapping("/clear")
    public R clear() {
        mallProductInfoService.clear();
        return R.ok();
    }


    /**
     * 查询地址列表
     */
    @GetMapping("/fetchAddressList")
    public R fetchAddressList() {
        List<MallProductAddrInfo> mallProductAddrInfoList = mallProductInfoService.fetchAddressList();
        return R.ok(mallProductAddrInfoList);
    }

    /**
     * 添加地址
     */
    @PostMapping("/addAddress")
    public R addAddress(@RequestBody MallProductAddrInfo mallProductAddrInfo) {
        mallProductInfoService.addAddress(mallProductAddrInfo);
        return R.ok();
    }


    /**
     * 地址解析
     * @param address
     * @return
     */
    @GetMapping("/analyzeAddress")
    public R<MallProductAddrInfo> analyzeAddress(@RequestParam("address") String address){
        return R.ok(mallProductInfoService.analyzeAddress(address));
    }


    /**
     * 根据关键字查询商品
     */
    @GetMapping("/search")
    public R<PageInfo<MallProductInfoNew>> search(@RequestParam("keyword") String keyword,
                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                   @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MallProductInfoNew> mallProductUserInfoList = mallProductInfoService.search(keyword,pageNum,pageSize);
        PageInfo<MallProductInfoNew> mallProductUserInfoPageInfo = new PageInfo<>(mallProductUserInfoList);
        return R.ok(mallProductUserInfoPageInfo);
    }


    /**
     * 人气推荐列表
     */
    @GetMapping("/hotProductList")
    public R<PageInfo<MallProductInfoNew>> hotProductList(@RequestParam(value = "pageNum", defaultValue = "2") Integer pageNum,
                                                            @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MallProductInfoNew> mallProductUserInfoList = mallProductInfoService.hotProductList(pageNum,pageSize);
        PageInfo<MallProductInfoNew> mallProductUserInfoPageInfo = new PageInfo<>(mallProductUserInfoList);
        return R.ok(mallProductUserInfoPageInfo);
    }

}
